<template>
  <div class="dashboard-container">
    <div class="container">
      <div class="tableBar">
        <div class="tableLab">
          <el-button
            type="primary"
            style="margin-left: 15px; color: white; background-color: #333333"
            @click="addRess('add')"
          >
            + 新建地址
          </el-button>
        </div>
      </div>
      <el-table
        v-if="tableData.length"
        :data="tableData"
        stripe
        class="tableBox"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="25" />
        <el-table-column prop="id" label="id" />

        <el-table-column prop="districtName" label="区" />
        <el-table-column prop="streetName" label="街道"> </el-table-column>
        <el-table-column prop="villageName" label="具体地址" />

        <el-table-column label="操作" width="250" align="center">
          <template slot-scope="scope">
            <el-button
              type="text"
              size="small"
              class="blueBug"
              @click="addRess(scope.row)"
            >
              修改
            </el-button>
            <el-button
              type="text"
              size="small"
              class="delBut"
              @click="deleteHandle('单删', scope.row.id)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>
      <Empty v-else :is-search="isSearch" />
      <el-pagination
        v-if="counts > 10"
        class="pageList"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="counts"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script lang="ts">
import { Component, Vue } from 'vue-property-decorator'
import HeadLable from '@/components/HeadLable/index.vue'
import { deleteAddress, getAddressPage } from '@/api/address'
import InputAutoComplete from '@/components/InputAutoComplete/index.vue'
import Empty from '@/components/Empty/index.vue'
import request from '@/utils/request'

@Component({
  name: 'DishType',
  components: {
    HeadLable,
    InputAutoComplete,
    Empty,
  },
})
export default class extends Vue {
  private input: any = ''
  private counts: number = 0
  private page: number = 1
  private pageSize: number = 10
  private checkList: string[] = []
  private tableData: [] = []
  private dishState = ''
  private categoryId = ''
  private isSearch: boolean = false
  $router: any
  $message: any
  selectedData: any

  created() {
    this.init()
  }

  initProp(val) {
    this.input = val
    this.initFun()
  }

  initFun() {
    this.page = 1
    this.init()
  }

  private async init(isSearch?) {
    this.isSearch = isSearch
    await getAddressPage({
      page: this.page,
      pageSize: this.pageSize,
    })
      .then((res) => {
        console.log(res)
        if (res.data.code === 1) {
          this.tableData = res.data && res.data.data && res.data.data.records
          this.counts = Number(res.data.data.total)
        }
      })
      .catch((err) => {
        this.$message.error('请求出错了：' + err.message)
      })
  }
  // 调用删除方法
  private deleteHandle = (type, id) => {
    if (type === '批量') {
      // 批量删除
      const selectedIds = this.selectedData.map((item) => item.id)
      if (selectedIds.length === 0) {
        // 如果没有选中任何数据，则提示用户选择要删除的数据
        this.$message.error('请选择要删除的数据')
        return
      }
      this.$confirm('确定删除选中的数据吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          // 调用删除接口
          Promise.all(selectedIds.map((id) => deleteAddress(id)))
            .then(() => {
              this.$message.success('删除成功')
              // 刷新数据列表
              this.init()
            })
            .catch((error) => {
              console.log(error)
              this.$message.error('删除失败')
            })
        })
        .catch(() => {
          // 用户点击取消按钮，不执行任何操作
        })
    } else if (type === '单删') {
      // 单个删除
      this.$confirm('确定删除该条数据吗？', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning',
      })
        .then(() => {
          // 调用删除接口
          deleteAddress(id)
            .then(() => {
              this.$message.success('删除成功')
              // 刷新数据列表
              this.init()
            })
            .catch((error) => {
              console.log(error)
              this.$message.error('删除失败')
            })
        })
        .catch(() => {
          // 用户点击取消按钮，不执行任何操作
        })
    }
  }
  // 添加更改
  private addRess(st: any) {
    if (st === 'add') {
      this.$router.push({ path: '/address/add' })
    } else {
      this.$router.push({ path: '/address/add', query: { id: st.id } })
    }
  }
  // 全部操作
  private handleSelectionChange(val: any) {
    let checkArr: any[] = []
    val.forEach((n: any) => {
      checkArr.push(n.id)
    })
    this.checkList = checkArr
  }

  private handleSizeChange(val: any) {
    this.pageSize = val
    this.init()
  }

  private handleCurrentChange(val: any) {
    this.page = val
    this.init()
  }
}
</script>
<style lang="scss">
.el-table-column--selection .cell {
  padding-left: 10px;
}
</style>
<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;

    .container {
      background: #fff;
      position: relative;
      z-index: 1;
      padding: 30px 28px;
      border-radius: 30px;

      //查询黑色按钮样式
      .normal-btn {
        background: #333333;
        color: white;
        margin-left: 20px;
      }

      .tableBar {
        margin-bottom: 20px;

        .tableLab {
          display: inline-block;
          float: right;

          span {
            cursor: pointer;
            display: inline-block;
            font-size: 14px;
            padding: 0 20px;
            color: $gray-2;
          }
        }
      }

      .tableBox {
        width: 100%;
        border: 1px solid $gray-5;
        border-bottom: 0;
      }

      .pageList {
        text-align: center;
        margin-top: 30px;
      }
    }
  }
}
</style>
